var redisDb = require("../../utils/db/redisDb");
var Promise = require('promise');
var accountDao = require('../dao/AccountDao');
var wechatRest = require('../../utils/wechatRest');
var en_wechatRest = require('../../utils/en_wechatRest');
var config = require('../../../config');
var logger = require('log4js').getLogger('AccountService');

var AccountService = {};


// 验证access_token是否失效
AccountService.verifyAccessToken = function (account) {
    return new Promise(function (fulfill, reject) {
        redisDb.get("WECHAT_ACCESSTOKEN_" + account.id).done(function (obj) {
            if (obj) {
                var tokenObj = JSON.parse(obj);
                (new Date().getTime() - parseInt(tokenObj.time) < 7000000) ? account.accessToken = tokenObj.accessToken : account.accessToken = null;
            }else{
                account.accessToken = null;
            }
            if (!account.accessToken) {
                wechatRest.getAccessToken(account.appId, account.appSecret).done(function (accessToken) {
                    account.accessToken = accessToken;
                    var tokenObj_ = {accessToken: accessToken, time: new Date().getTime()};
                    redisDb.setex("WECHAT_ACCESSTOKEN_" + account.id, JSON.stringify(tokenObj_), 7200);
                    fulfill(account);
                }, function (err) {
                    reject(err);
                });
            } else {
                fulfill(account);
            }

        }, function (err) {
            reject(err)
        });
    });
};

// 验证企业access_token是否失效
AccountService.verifyEn_AccessToken = function (account) {
    return new Promise(function (fulfill, reject) {
        redisDb.get("WECHAT_EN_ACCESSTOKEN_" + account.id).done(function (obj) {
            if (obj) {
                var tokenObj = JSON.parse(obj);
                (new Date().getTime() - parseInt(tokenObj.time) < 7000000) ? account.accessToken = tokenObj.accessToken : account.accessToken = null;
            }else{
                account.accessToken = null;
            }
            if (!account.accessToken) {
                en_wechatRest.getAccessToken(account.appId, account.appSecret).done(function (accessToken) {
                    account.accessToken = accessToken;
                    var tokenObj_ = {accessToken: accessToken, time: new Date().getTime()};
                    redisDb.setex("WECHAT_EN_ACCESSTOKEN_" + account.id, JSON.stringify(tokenObj_), 7200);
                    fulfill(account);
                }, function (err) {
                    reject(err);
                });
            } else {
                fulfill(account);
            }

        }, function (err) {
            reject(err)
        });
    });
};

//验证jsapi_ticket
AccountService.verifyJsTicket = function (account) {
    return new Promise(function (fulfill, reject) {
        redisDb.get("WECHAT_JSAPI_TICKET_" + account.id).done(function (obj) {
            if (obj) {
                var ticketObj = JSON.parse(obj);
                (new Date().getTime() - parseInt(ticketObj.time) < 7200000) ? account.jsTicket = ticketObj.jsTicket : account.jsTicket = null;
            }else{
                account.jsTicket = null;
            }
            if (!account.jsTicket) {
                wechatRest.getJsTicket(account.accessToken).then(function (jsTicket) {
                    account.jsTicket = jsTicket;
                    var ticketObj_ = {jsTicket: jsTicket, time: new Date().getTime()};
                    redisDb.setex("WECHAT_JSAPI_TICKET_" + account.id, JSON.stringify(ticketObj_), 7200);
                    fulfill(account);
                }, function (err) {
                    reject(err);
                });
            } else {
                fulfill(account);
            }

        }, function (err) {
            reject(err);
        });
    });
};


AccountService.getById = function (id) {
    return accountDao.getById(id);
};

// 获取默认的微信账户
AccountService.getDefaultAccount = function () {
    return accountDao.getDefaultAccount();
};

module.exports = AccountService;